<?php

namespace App\Providers;

use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Illuminate\Support\ServiceProvider;
use App\Library\Logs\RotatingFileHandler;


class LogServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->mergeConfigFrom(config_path('log.php'), 'log');
        $this->app->configureMonologUsing(function (Logger $logger) {
            $handlers = $this->getLogHandlers();
            foreach ($handlers as $handler) {
                $logger->pushHandler($handler);
            }
            return $logger;
        });
    }

    protected function getLogHandlers()
    {
        $request = app('request');
        $file_path = "laravel.log";
        if(!empty($request->path())){
            $file_path = implode("_",explode("/",$request->path())).'.log';
        }
        $handlers = array();
        $logFilePath = config("log.file.path");
        $logFilePath = $logFilePath."/".date("Ym")."/".date("d")."/".$file_path ;
        $logFileRotate = (int)(config('log.file.rotate'));
        $logLevel = config('log.level') ?: 'info';
        $handler = new RotatingFileHandler($logFilePath, $logFileRotate, $logLevel, true, 0777);
        $handler->setFormatter(new LineFormatter(null, null, true, true));
        $handlers[] = $handler;
        return $handlers;
    }
}
